package com.ihrm.company.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.response.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//1 解决跨域
@CrossOrigin
//2. 声明restContoller
@RestController
//3 设置父路径
@RequestMapping(value = "/company") // company/department
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;  //企业

    @Autowired
    private CompanyService companyService; //


    /**
     * 保存
     */
    @RequestMapping(value = "/department",method = RequestMethod.POST)
    public Result save(@RequestBody Department department){
        //1. 设置保存企业的id
        /**
         * 企业id： 目前使用固定值1
         */
//        String companyId = "1";  因为BaseController 统一定义公共
        department.setCompanyId(companyId);
        //2. 调用service完成保存企业
        departmentService.save(department);
        // 3. 构造返回结果
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 查询企业部门列表
     * 指定企业id
     */
    @RequestMapping(value = "/department",method = RequestMethod.GET)
    public Result findAll(String id){
        //指定企业id
//        String companyid = "1";
        Company company = companyService.findById(companyId);
        // 调用查询方法
        List<Department> list = departmentService.findAll(companyId);
        //构造返回结果
        DeptListResult deptListResult = new DeptListResult(company,list);

        return new Result(ResultCode.SUCCESS,deptListResult);
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/department/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id){
        Department department = departmentService.findbyid(id);
        return new Result(ResultCode.SUCCESS,department);
    }

    /**
     * 根据id修改
     */
    @RequestMapping(value = "/department/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id,@RequestBody Department department){
        //1. 设置修改的部门id
        department.setId(id);
        //2. 调用service更新
        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 根据id删除
     */
    @RequestMapping(value = "/department/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id){
        departmentService.deletebyid(id);
        return new Result(ResultCode.SUCCESS);
    }


}








